package com.italicatab;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.conn.ConnectTimeoutException;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class CheckInternetConnection {
	public Context context = null;

	// public ProgressDialog dialogProcess = null;

	public CheckInternetConnection(Context ctx) {
		this.context = ctx;
	}

	public boolean CheckInternetOLD() {
		ConnectivityManager connec = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		android.net.NetworkInfo wifi = connec
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		android.net.NetworkInfo mobile = connec
				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

		if (wifi.isConnected()) {
			return true;
		} else if (!mobile.isConnected()) {
			// Toast.makeText(context, "No Internet Connection", 5000).show();
			return false;
		} else {
			try {
				URL url = new URL("http://www.google.com");
				HttpURLConnection urlc = (HttpURLConnection) url
						.openConnection();
				urlc.setRequestProperty("User-Agent", "Test");
				urlc.setRequestProperty("Connection", "close");
				urlc.setConnectTimeout(2000); // This is time limit if the
				// connection time limit
				urlc.connect();
				if (urlc.getResponseCode() == 200) {
					// Log.e("","Getting responce Code  200 ");
					return true;
				}
			} catch (ConnectTimeoutException e) {
				return false;
			} catch (MalformedURLException e1) {
				e1.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return false;
		}

	}

	public boolean CheckInternet() {
		return isOnline();
	}

	public Boolean isOnline() {
		if (isNetAvailable(context))
			return true;
		else {
			try {
				URL url = new URL("http://www.google.com");
				HttpURLConnection urlc = (HttpURLConnection) url
						.openConnection();
				urlc.setRequestProperty("User-Agent", "Test");
				urlc.setRequestProperty("Connection", "close");
				urlc.setConnectTimeout(3000); // This is time limit if the
				// connection time limit
				urlc.connect();
				if (urlc.getResponseCode() == 200) {
					return true;
				}
			} catch (MalformedURLException e1) {
				e1.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	public synchronized static boolean isNetAvailable(Context context) {

		try {
			boolean isNetAvailable = false;
			if (context != null) {
				ConnectivityManager mgr = (ConnectivityManager) context
						.getSystemService(Context.CONNECTIVITY_SERVICE);
				if (mgr != null) {
					boolean mobileNetwork = false;
					boolean wifiNetwork = false;
					boolean wiMaxNetwork = false;

					boolean mobileNetworkConnecetd = false;
					boolean wifiNetworkConnecetd = false;
					boolean wiMaxNetworkConnected = false;

					NetworkInfo mobileInfo = mgr
							.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
					NetworkInfo wifiInfo = mgr
							.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
					NetworkInfo wiMaxInfo = mgr
							.getNetworkInfo(ConnectivityManager.TYPE_WIMAX);

					if (mobileInfo != null)
						mobileNetwork = mobileInfo.isAvailable();

					if (wifiInfo != null)
						wifiNetwork = wifiInfo.isAvailable();

					if (wiMaxInfo != null)
						wiMaxNetwork = wiMaxInfo.isAvailable();

					if (wifiNetwork == true)
						wifiNetworkConnecetd = wifiInfo
								.isConnectedOrConnecting();
					if (mobileNetwork == true)
						mobileNetworkConnecetd = mobileInfo
								.isConnectedOrConnecting();
					if (wiMaxNetwork == true)
						wiMaxNetworkConnected = wiMaxInfo
								.isConnectedOrConnecting();

					isNetAvailable = (mobileNetworkConnecetd
							|| wifiNetworkConnecetd || wiMaxNetworkConnected);
				}
			}
			return isNetAvailable;
		} catch (NullPointerException e) {
			return false;
		} catch (Exception e) {
			return false;
		}
	}

}